﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Globalization;
using System.Web.Script.Services;
using System.Web.Security;
using System.Web.Services;
using System.Collections.ObjectModel;
using System.Text;
using System.Configuration;

namespace TractRecord.Administration
{
    public partial class Roles : TractRecordBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static ArrayList GetAllRoles()
        {
            ArrayList values = new ArrayList();

            string[] roles = System.Web.Security.Roles.GetAllRoles();

            foreach (string role in roles)
            {
                values.Add(new
                {
                    RoleName = role,
                    UsersInRole = System.Web.Security.Roles.GetUsersInRole(role).Count()
                });
            }
            
            return values;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void CreateRole(string RoleName)
        {
            System.Web.Security.Roles.CreateRole(RoleName);
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void DeleteRole(string RoleName)
        {
            System.Web.Security.Roles.DeleteRole(RoleName, false);
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void AddToRole(string RoleName, string UserId)
        {
            Guid id = new Guid(UserId);

            MembershipUser user = Membership.GetUser(id);

            System.Web.Security.Roles.AddUserToRole(user.UserName, RoleName);
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static ArrayList UsersInRole(string RoleName)
        {
            ArrayList data = new ArrayList();

            string[] usernames = System.Web.Security.Roles.GetUsersInRole(RoleName);

            foreach (string username in usernames)
            {
                MembershipUser user = Membership.GetUser(username);

                data.Add(
                    new
                    {
                        UserId = user.ProviderUserKey,
                        UserName = user.UserName
                    });
            }

            return data;
          
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void RemoveFromRole(string RoleName, string UserId)
        {
            Guid id = new Guid(UserId);

            MembershipUser user = Membership.GetUser(id);

            System.Web.Security.Roles.RemoveUserFromRole(user.UserName, RoleName);
        }
        
    }
}